﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ncp.Business;

namespace ncp.Web.Shop
{
    public partial class product : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(product));
            hdProductType.Value = Request.QueryString["product_type"];
        }
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetProductCount(string shop,string proType)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            if (CommonBll.IsLegitimateShopId(shop))
            {
                int product_type = 0;
                string where = " Client_Id=" + CommonBll.GetTheRealId(shop).ToString();
                if (proType != "" && int.TryParse(proType, out product_type))
                {
                    where += " and GC_Id in (select GC_Id from bi_GoodsClass where GC_Level=3 and GC_Status=1 and GC_PId=" + proType + ")";
                }
                var count = ProductInfoBll.GetProductInfoCount(where, CommonConst.DB_Name);
                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", count);
            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "0");
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetProductList(int pageIndex, int pageSize, string shop, string proType)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            if (CommonBll.IsLegitimateShopId(shop))
            {
                int product_type = 0;
                var dicProduct = new Dictionary<string, object>();
                dicProduct.Add("Client_Id", CommonBll.GetTheRealId(shop).ToString());
                if (proType != ""&&int.TryParse(proType, out product_type))
                {
                    dicProduct.Add("more", "GC_Id in (select GC_Id from bi_GoodsClass where GC_Level=3 and GC_Status=1 and GC_PId=" + proType + ")");
                }
                try
                {
                    StringBuilder builder = new StringBuilder();
                    var dataTable = ProductInfoBll.GetProductInfoTable(pageIndex, pageSize, "*", "Product_StartTime desc", dicProduct, CommonConst.DB_Name);
                    foreach (DataRow row in dataTable.Rows)
                    {
                        builder.AppendFormat("<dl><dt><a href=\"/product/{0}.html\" target=\"_blank\"><img src=\"{1}\" /></a></dt>", EncryptHelper.EncryptString(row["Product_Id"].ToString()), row["Product_Picture"]);
                        builder.AppendFormat("<dd style='text-align: center;'>{0}</dd></dl>", row["Product_Title"]);
                    }
                    builder.Append("<div style=\"clear:both;\"></div>");
                    jsonResult.Add("isreload", true);
                    jsonResult.Add("msg", builder.ToString());
                }
                catch (Exception ex)
                {
                    jsonResult.Clear();
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", ex.Message);
                }

            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "0");
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
